import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
  },
  {
    path: '/layout',
    component: () => import('@/views/Layout'),
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home')
      },
      {
        path: 'article',
        component: () => import('@/views/Article')
      },
      {
        path: 'publish',
        component: () => import('@/views/Publish')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由守卫
const token = getToken()
router.beforeEach((to, from, next) => {
  if (!token && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})
export default router
